import User, { UserInfo } from "../../../utils/user";
import { getInterfaceRoute } from '../../../utils/request/route';
import { uploadlist, fileUrl, SAddLook } from '../../../utils/request/api';
import msg from "../../../utils/msg";
var user = new User;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: user.getInfo() as UserInfo,
    list: [] as any,
    showlist: [] as any,
  },

  trigger(value: any) {
    this.triggerEvent('listOutput', value);
  },

  // 点击上传证明材料
  onClickList(e: any) {
    const index: number = e.currentTarget.dataset.index;
    const item: any = this.data.list[index];
    this.trigger(item);
    console.log(item);
    // 选择文件
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success: (res) => {
        const tempFilePath = res.tempFiles[0].path;
        this._uploadFile(tempFilePath, item.id, this.data.info.code);
        if (item.islook == 0) {
          SAddLook({ a_id: item.id, s_code: this.data.info.code });
          uploadlist({ s_name: this.data.info.name, s_code: this.data.info.code })
            .then((res: any) => {
              this.setData({
                list: res.data.data,
              })
            });
        }
      },
    });
  },

  //将文件上传到服务器
  _uploadFile(filePath: string, id: number, s_code: string) {
    wx.uploadFile({
      url: getInterfaceRoute('upload'),
      filePath: filePath,
      name: 'prize',
      success: (res) => {
        const data = JSON.parse(res.data).data;
        fileUrl({ a_id: id, s_code: s_code, filepath: data.file_path });
        msg.show('上传成功', '请勿重复操作');
      },
      fail: (error) => {
        console.error(error);
      },
    });
  },

  //搜索活动
  onSearch(e: any) {
    let input = e.detail.value;
    this.setData({
      input: input,
    });
    let data = this.data.list;
    let data2 = [] as any;
    data.forEach((item: any, index: any) => {
      if (item.title.indexOf(input) !== -1) {
        data2.push(item);
      }
    });
    this.setData({
      showlist: data2,
    })
  },

  //返回
  onClickLeft() {
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function () {
    await uploadlist({ s_name: this.data.info.name, s_code: this.data.info.code })
      .then(async (res: any) => {
        this.setData({
          list: res.data.data,
          showlist: res.data.data,
        })
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    await uploadlist({ s_name: this.data.info.name, s_code: this.data.info.code })
      .then(async (res: any) => {
        this.setData({
          list: res.data.data,
          showlist: res.data.data,
        })
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})